##########################################################################################

library(ktplots)
library(optparse)
library(data.table)
library(dplyr)

##########################################################################################

option_list <- list(
    make_option(c("--gene_list_file"), type = "character"),
    make_option(c("--tf_pathway_file"), type = "character"),
    make_option(c("--pathway_combine_file"), type = "character"),
    make_option(c("--type"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){

  type <- "pct_0.25.cor_0.5"
  gene_list_file <- "~/20231121_singleMuti/results/celltype_plot/trajectory/positive/pct_0.25.cor_0.5/plotTrajectoryHeatmap.GEM_MM.tsv"
  tf_pathway_file <- "~/20231121_singleMuti/config/pathway_combine.csv"
  pathway_combine_file <- "~/20231121_singleMuti/results/celltype_plot/tf_regulators_all_germ_pathway/germ_TF-TargetGene.GO.tsv"
  out_path <- "~/20231121_singleMuti/results/celltype_plot/tf_regulators_all_germ_pathway_plot"

}

##########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

out_path <- opt$out_path
pathway_combine_file <- opt$pathway_combine_file
type <- opt$type
gene_list_file <- opt$gene_list_file
tf_pathway_file <- opt$tf_pathway_file

dir.create(out_path , recursive = T)

##########################################################################################

pathway_combine <- data.frame(fread(pathway_combine_file))
tf_pathway <- data.frame(fread(tf_pathway_file))
gene_list <- data.frame(fread(gene_list_file , header = T))$gene

##########################################################################################
## 只提取感兴趣的通路
raw.df <- merge( pathway_combine , tf_pathway , by.x = "Term" , by.y = "similar.pathways" )
raw.df$ER <- raw.df$Significant/raw.df$Expected

## combine相同的通路
raw.df <- raw.df %>%
group_by( TF , combine_name ) %>%
summarize( pvalue = min(pvalue) , ER = max(ER) )

##########################################################################################
## 只提取感兴趣的TF
gene_list <- sapply(strsplit(gene_list , "_") , "[" , 1 )
raw.df <- subset( raw.df , TF %in% gene_list )
raw.df$TF <- factor( raw.df$TF , levels = gene_list[length(gene_list):1] , order = T )

##########################################################################################
## 通路顺序
pathway_order <- unique(tf_pathway$combine_name)
raw.df$combine_name <- factor( raw.df$combine_name , levels = pathway_order , order = T )

##########################################################################################

color_palette = c("#313695", "#4575B4", "#ABD9E9", "#FFFFB3", "#FDAE61", "#F46D43", "#D73027", "#A50026")
raw.df$p_log10 <- -log10(raw.df$pvalue)
raw.df$p_log10 <- ifelse( raw.df$p_log10 > 5 , 5 , raw.df$p_log10 )
raw.df$ER_log2 <- log10(raw.df$ER)

p <- ggplot(raw.df,aes( x = combine_name , y = TF))+
    geom_point(aes(size=ER_log2,color=p_log10))+
    scale_color_gradientn("-log10(p value)",colors = color_palette)+
    scale_size_continuous("enrichment ratio")+
    theme_bw()+
    theme(
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      panel.background = element_blank(),
      panel.border = element_blank(),
      axis.title = element_blank(),
      axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1 , size = 13 , color = "black" ) ,
      axis.text.y = element_text(size = 11 , color = "black"),
      axis.line = element_line(size = 0.5),
      axis.ticks.length = unit(0.15,"cm") ,
      plot.margin = margin( 1 , 2 , 1 , 10 , "lines") #分别对应上右下左
    )

out_name <- paste0( out_path , "/tf_pathway." , type , ".pdf" )
ggsave( out_name , p , width = 45/3.3 , height = length(gene_list)/4 )

out_name <- paste0( out_path , "/tf_pathway." , type , ".tsv" )
write.table( raw.df , out_name , sep = "\t" , row.names = F , quote = F )
